package 二叉树.m04.d04.t01;

import 二叉树.m03.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {

        int start = 0, end = 0;
        TreeNode[] deque = new TreeNode[2001];
        List<List<Integer>> arr = new ArrayList<>();
        if(root == null) return arr;
        deque[end++] = root;
        while(end>start) {
            int num = end - start;
            List<Integer> l = new ArrayList<>();
            for(int i = 0; i < num; i++) {
                TreeNode node = deque[start++];
                // System.out.println(node.val);
                l.add(node.val);
                if(node.left != null) {
                    System.out.println(node.left.val);
                    deque[end++] = node.left;
                }
                if(node.right != null) {
                    // System.out.println(node.right.val);
                    deque[end++] = node.right;
                }
            }

            arr.add(l);

        }
        return arr;
    }
}